﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AMS.Database;
using AMS.Business;
using AMS.LINQ;

namespace AMS
{
    public partial class frmAnalysisMethods : BaseForm, IUpdatableForm
    {
        AnalysisMethodClass analysisMethod;
        AnalysisMethodGroupClass analysisMethodGroup;
        int loadedAnalysisMethod = 0;

        #region Form Events
        public frmAnalysisMethods()
        {
            InitializeComponent();

            base.isSingleton = true;
            base.guid = new Guid("{681220D6-E357-4293-BA06-37D721D60C33}");
        }

        private void AnalysisMethods_Load(object sender, EventArgs e)
        {
            try
            {
                BindListboxData();
                BindComboboxData();
                BindData();
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);
            }
            
        }
        #endregion

        #region Button Events

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.DestroyForm(this);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            try
            {
                analysisMethod = new AnalysisMethodClass();

                loadedAnalysisMethod = 0;
                txtNameEnglish.Text = String.Empty;
                txtNameFrench.Text = String.Empty;
                txtNameGerman.Text = String.Empty;
                txtName.Text = String.Empty;
                txtName.Focus();
                txtVersion.Text = String.Empty;
                chkActive.Checked = false;
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                analysisMethod.Save();
                BindData();

                loadedAnalysisMethod = analysisMethod.AnalysisMethodID;

                base.internalFormManager.RefreshData(this);
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);   
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                analysisMethod.Delete(loadedAnalysisMethod);
                BindData();

                base.internalFormManager.RefreshData(this);
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);
            }
        }

        private void btnNewAnalisys_Click(object sender, EventArgs e)
        {
            try
            {
                analysisMethodGroup = new AnalysisMethodGroupClass();

                txtAnalysisMethodGroupName.Text = String.Empty;
                txtAnalysisMethodGroupName.Focus();
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);
            }
        }

        private void btnSaveAnalysis_Click(object sender, EventArgs e)
        {
            try
            {
                analysisMethodGroup.Save();
                BindListboxData();
                BindComboboxData();
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);
            }
        }

        private void btnDeleteAnalysis_Click(object sender, EventArgs e)
        {
            try
            {
                bool isDeleted = analysisMethodGroup.Delete();

                if (!isDeleted)
                {
                    MessageBox.Show("Item kan niet verwijderd worden omdat het nog in gebruik is.","Verwijderen",MessageBoxButtons.OK);
                    return;
                }

                analysisMethodGroup = null;
                BindListboxData(); 
                BindComboboxData();
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);
            }
        }
        #endregion

        #region Combobox
        private void BindComboboxData()
        {
            var analysisMethodGroup = DataAccess.GetAnalysisMethodGroupList();

            cboAnalysisGroup.DataSource = null;
            cboAnalysisGroup.ValueMember = "AnalysisMethodGroupID";
            cboAnalysisGroup.DisplayMember = "AnalysisMethodGroupName";
            cboAnalysisGroup.DataSource = analysisMethodGroup;
        }

        private void cboAnalysisGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (analysisMethod != null)
            {
                analysisMethod.AnalysisMethodGroupID = Convert.ToInt32(cboAnalysisGroup.SelectedValue);
            }
        }

        #endregion

        #region Listbox
        private void BindListboxData()
        {
            try
            {
                var analysisMethodGroup = DataAccess.GetAnalysisMethodGroupList();

                lstAnalysis.DataSource = null;
                lstAnalysis.ValueMember = "AnalysisMethodGroupID";
                lstAnalysis.DisplayMember = "AnalysisMethodGroupName";
                lstAnalysis.DataSource = analysisMethodGroup;
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);
            }
        }

        private void lstAnalysis_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (lstAnalysis.SelectedItem != null)
                {
                    AnalysisMethodGroup analysisMethodGroupLINQ = (AnalysisMethodGroup)lstAnalysis.SelectedItem;

                    analysisMethodGroup = new AnalysisMethodGroupClass();
                    analysisMethodGroup.Load(analysisMethodGroupLINQ.AnalysisMethodGroupID);

                    txtAnalysisMethodGroupName.Text = analysisMethodGroup.AnalysisMethodGroup;
                }
                else
                {
                    analysisMethodGroup = new AnalysisMethodGroupClass();
                    txtAnalysisMethodGroupName.Text = String.Empty;
                }
            }
            catch (Exception ex)
            {
                base.internalFormManager.ShowError(ex);
            }
        }
        #endregion

        #region Datagrid

        private void dgrAnalysisMethods_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                // Get the value from the first column
                DataGridViewCell cell = dgrAnalysisMethods.Rows[e.RowIndex].Cells[0];
                LoadData((Int32)cell.Value);

                DataGridViewCell referencedRequestCell = dgrAnalysisMethods.Rows[e.RowIndex].Cells[4];
                SetButtons(referencedRequestCell);
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void dgrAnalysisMethods_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = dgrAnalysisMethods.CurrentRow.Cells[0];
            if (cell.Value.ToString() != String.Empty)
            {
                LoadData((Int32)cell.Value);

                DataGridViewCell referencedRequestCell = dgrAnalysisMethods.CurrentRow.Cells[4];
                SetButtons(referencedRequestCell);
            }
        }

        private void BindData()
        {
            dgrAnalysisMethods.DataSource = null; ;
            BindingSource bindingSource = new BindingSource();
            dgrAnalysisMethods.DataSource = bindingSource;
            bindingSource.DataSource = DataAccess.GetAnalysisMethodList().Tables[0];

            FormatDataGrid();
        }

        private void FormatDataGrid()
        {
            DataGridViewColumn col;

            col = dgrAnalysisMethods.Columns["colID"];
            if (col != null)
            {
                col.Visible = false; // ID    
            }
            

            col = dgrAnalysisMethods.Columns["colName"];
            if (col != null)
            {
                col.Width = 150; // Name
            }

            col = dgrAnalysisMethods.Columns["colVersion"];
            if (col != null)
            {
                col.Width = 150; // Version
            }

            col = dgrAnalysisMethods.Columns["colActive"];
            if (col != null)
            {
                col.Visible = true; // Active
                col.Width = 40;
            }

            col = dgrAnalysisMethods.Columns["colReferencedRequests"];
            if (col != null)
            {
                col.Width = 100; // Referenced Requests
            }
        }

        private void SetButtons(DataGridViewCell cell)
        {
            if ((Int32)cell.Value == 0)
            {
                // Enable the delete button
                btnDelete.Enabled = true;
            }
            else
            {
                btnDelete.Enabled = false;
            }
        }
        #endregion

        #region Input and Validation

        private void LoadData(Int32 analysisMethodID)
        {
            try
            {
                analysisMethod = new AnalysisMethodClass();
                analysisMethod.Load(analysisMethodID);

                loadedAnalysisMethod = analysisMethod.AnalysisMethodID;
                txtName.Text = analysisMethod.AnalysisMethodName;
                txtNameEnglish.Text = analysisMethod.AnalysisMethodNameEN;
                txtNameFrench.Text = analysisMethod.AnalysisMethodNameFR;
                txtNameGerman.Text = analysisMethod.AnalysisMethodNameDE;
                txtVersion.Text = analysisMethod.AnalysisMethodVersion;
                chkActive.Checked = analysisMethod.Active;

                // Find the corresponding item in the combobox
                cboAnalysisGroup.SelectedValue = analysisMethod.AnalysisMethodGroupID;
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void Input_Validating(object sender, EventArgs e)
        {
            Control currentControl = (Control)sender;

            try
            {
                switch (currentControl.Name)
                {
                    case "txtName":
                        analysisMethod.AnalysisMethodName = txtName.Text;
                        break;
                    case "txtNameGerman":
                        analysisMethod.AnalysisMethodNameDE = txtNameGerman.Text;
                        break;
                    case "txtNameFrench":
                        analysisMethod.AnalysisMethodNameFR = txtNameFrench.Text;
                        break;
                    case "txtNameEnglish":
                        analysisMethod.AnalysisMethodNameEN = txtNameEnglish.Text;
                        break;
                    case "txtVersion":
                        analysisMethod.AnalysisMethodVersion = txtVersion.Text;
                        break;
                    case "txtAnalysisMethodGroupName":
                        analysisMethodGroup.AnalysisMethodGroup = txtAnalysisMethodGroupName.Text;
                        break;
                }

                errAnalysisMethods.SetError(currentControl, "");
                btnSave.Enabled = true;
            }
            catch (Exception ex)
            {
                HandleValidationException(currentControl, ex.Message);
            }
        }

        private void chkActive_CheckedChanged(object sender, EventArgs e)
        {
            if (analysisMethod != null)
            {
                analysisMethod.Active = chkActive.Checked;
            }
        }

        #endregion

        #region Error handling
        private void HandleValidationException(Control currentControl, String value)
        {
            errAnalysisMethods.SetError(currentControl, value);
            // Disable the save button
            btnSave.Enabled = false;
        }
        #endregion

        #region IUpdatableForm Members

        void IUpdatableForm.RefreshData()
        {
            BindData();
        }

        #endregion

    }
}
